package com.xmtlxx.web.controller.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.web.controller.tms.service.IOrderLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.tms.domain.OrderLog;
import com.xmtlxx.tms.domain.bo.OrderLogBo;
import com.xmtlxx.tms.domain.vo.OrderLogVo;
import com.xmtlxx.tms.mapper.OrderLogMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 订单日志Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OrderLogServiceImpl implements IOrderLogService {

    private final OrderLogMapper baseMapper;

    /**
     * 查询订单日志
     *
     * @param id 订单日志主键
     * @return 订单日志
     */
    @Override
    public OrderLogVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectOrderLogVoByID(id);
    }

    /**
     * 查询订单日志列表
     *
     * @param bo 订单日志
     * @return 订单日志
     */
    @Override
    public TableDataInfo<OrderLogVo> queryPageList(OrderLogBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<OrderLog> lqw = buildQueryWrapper(bo);
       // Page<OrderLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<OrderLogVo> result = baseMapper.selectPageOrderLogList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单日志列表
     *
     * @param bo 订单日志
     * @return 订单日志
     */
    @Override
    public List<OrderLogVo> queryList(OrderLogBo bo) {
       // LambdaQueryWrapper<OrderLog> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectOrderLogList(bo);
    }

    private LambdaQueryWrapper<OrderLog> buildQueryWrapper(OrderLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), OrderLog::getOrderId, bo.getOrderId());
        lqw.between(params.get("beginLogTime") != null && params.get("endLogTime") != null,
            OrderLog::getLogTime ,params.get("beginLogTime"), params.get("endLogTime"));
        lqw.eq(bo.getUserId() != null, OrderLog::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), OrderLog::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getLogType()), OrderLog::getLogType, bo.getLogType());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), OrderLog::getCreateBy, bo.getCreateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            OrderLog::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), OrderLog::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增订单日志
     *
     * @param bo 订单日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(OrderLogBo bo) {
        OrderLog add = BeanUtil.toBean(bo, OrderLog.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单日志
     *
     * @param bo 订单日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(OrderLogBo bo) {
        OrderLog update = BeanUtil.toBean(bo, OrderLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(OrderLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单日志
     *
     * @param ids 需要删除的订单日志主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
